from flask import Blueprint, session, render_template, redirect, request
from model import *
import uuid

blue = Blueprint ("blue", __name__)

"""===================================================初始化管理员====================================================="""


@blue.route ("/reg")
def reg ():
    user = User (username = "田玉亭", password = "xiaojian", phone = "13140060872",
                 isanmin = True)
    db.session.add (user)
    db.session.commit ()
    return redirect ("/admin/login")


"""===================================================首页展示========================================================"""


@blue.route ("/home")
def home ():
    if session.get ("username"):
        user = User.query.first ()
        return render_template ("admin/index.html", user = user)
    else:
        return redirect ("/admin/login")


"""===================================================登录页面========================================================"""


@blue.route ("/login", methods = ["GET", "POST"])
def login ():
    if request.method == "GET":
        return render_template ("admin/login.html")
    else:
        username = request.form.get ("username")
        password = request.form.get ("password")
        user = User.query.filter (username == username,password==password).first ()
        if user:
            session ["username"] = username
            return render_template ("admin/index.html", username = username)
        else:
            return render_template ("admin/login.html")


"""===================================================添加新闻分类====================================================="""


@blue.route ("/catelist")
def catelist ():
    new = Cate.query.all ()
    return render_template ("admin/news_type.html", new = new)


"""===================================================添加新闻分类====================================================="""


@blue.route ("/addnews", methods = ["GET", "POST"])
def addnew ():
    if request.method == "GET":
        return render_template ("admin/news_type_add.html")
    else:
        name = request.form.get ("name")
        cate = Cate (name = name)
        db.session.add (cate)
        db.session.commit ()
        return redirect ("/admin/newslist")


"""===================================================修改新闻分类====================================================="""


@blue.route ("/updatenews/<int:id>", methods = ["GET", "POST"])
def updatenews (id):
    cates = Cate.query.get (id)
    if request.method == "GET":
        return render_template ("admin/news_type_modify.html", cates = cates)
    carename = request.form.get ("name")
    cates.name = carename
    db.session.commit ()
    return redirect ("/admin/newslist")


"""===================================================删除新闻分类====================================================="""


@blue.route ("/deletenews/<int:id>")
def daletenews (id):
    cates = Cate.query.get (id)
    db.session.delete (cates)
    db.session.commit ()
    return redirect ("/admin/newslist")


"""===================================================退出============================================================"""


#
@blue.route ("/outlog")
def outlog ():
    session.clear ()
    return redirect ("/admin/login")


"""===================================================新闻列表========================================================"""


@blue.route ("/newslist")
def newslist ():
    newslist = News.query.filter (News.status != 2)
    return render_template ("admin/news_review.html", newslist = newslist)


"""===================================================审核新闻========================================================"""


@blue.route ("/detail",methods=["GET","POST"])
def detail ():
    if request.method == "GET":
        id = request.args.get ("id")
        news = News.query.filter (News.id == id).first ()
        return render_template ("admin/news_review_detail.html", news = news)
    else:
        id = request.form.get("news_id")
        status = request.form.get("action")
        reason = request.form.get("reason")
        print(id,status,reason)
        news = News.query.filter(News.id == id).first()

        news.status = status
        news.reason = reason
        db.session.commit()
        return redirect("/admin/newslist")


"""===================================================宏函数=========================================================="""


# 给模板提供了一个宏函数
def csrf_token ():
    token = uuid.uuid1 ()
    session ["csrf_token"] = token
    return token


blue.add_app_template_global (csrf_token, "csrf_token")
